//
//  SeparatorCellData.m
//  18Birdies
//
//  Created by Lc on 15/4/2.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "LcFoundation.h"
#import "SeparatorCell.h"
#import "SeparatorCellData.h"

@interface SeparatorCellData ()

@property(nonatomic, strong) UIImage* dottedLineImage;

@end

@implementation SeparatorCellData

+ (SeparatorCellData*)normalSeparatorDataWithLeftInset:(CGFloat)leftInset
                                       backgroundColor:
                                           (UIColor*)backgroundColor {
  SeparatorCellData* separatorCellData = [[self alloc] init];
  separatorCellData.cellHeight = [LcUIUtility pointsOfPixels:1];
  separatorCellData->_type = SEPARATORY_TYPE_NORMAL;
  separatorCellData->_leftInset = leftInset;
  separatorCellData->_rightInset = 0;
  separatorCellData->_lineColor = LC_COLOR_BLACKGROUND_GRAY;
  separatorCellData->_backgroundColor = backgroundColor;
  separatorCellData.clickable = NO;
  return separatorCellData;
}

+ (SeparatorCellData*)normalSeparatorDataWithLeftRightInsetHeight:(CGFloat)height
                                                        leftInset:(CGFloat)leftInset
                                                        rightInset:(CGFloat)rightInset
                                                  backgroundColor:(UIColor*)backgroundColor {
  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftInset:leftInset backgroundColor:backgroundColor];
  separatorCellData.cellHeight = height;
  separatorCellData->_rightInset = rightInset;
  separatorCellData->_type = SEPARATORY_TYPE_NORMAL_INSET;
  return separatorCellData;
}

+ (SeparatorCellData*)customSeparatorDataWithLeftInset:(CGFloat)leftInset
                                             lineColor:(UIColor*)lineColor
                                       backgroundColor:
                                           (UIColor*)backgroundColor {
  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftInset:leftInset backgroundColor:backgroundColor];
  separatorCellData->_lineColor = lineColor;
  return separatorCellData;
}

+ (SeparatorCellData*)customSeparatorDataWithLeftInset:(CGFloat)leftInset
                                            rightInset:(CGFloat)rightInset
                                             lineColor:(UIColor*)lineColor
                                       backgroundColor:(UIColor*)backgroundColor {
  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftInset:leftInset backgroundColor:backgroundColor];
  separatorCellData->_rightInset = rightInset;
  separatorCellData->_lineColor = lineColor;
  return separatorCellData;
}

+ (SeparatorCellData*)lightWhiteSeparatorDataWithLeftInset:(CGFloat)leftInset
                                           backgroundColor:
                                               (UIColor*)backgroundColor {
  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftInset:leftInset backgroundColor:backgroundColor];
  separatorCellData->_lineColor = [UIColor colorWithWhite:1 alpha:0.3];
  return separatorCellData;
}

+ (SeparatorCellData*)lightSandSeparatorDataWithHeight:(CGFloat)height
                                            showShadow:(BOOL)isShow {
  LC_CHECK_TRUE(height > 0);
  return [self separatorDataWithHeight:height
                                 color:LC_COLOR_BLACKGROUND_GRAY
                            showShadow:isShow];
}

+ (SeparatorCellData*)transparentSeparatorDataWithHeight:(CGFloat)height
                                              showShadow:(BOOL)isShow {
  LC_CHECK_TRUE(height > 0);
  return [self separatorDataWithHeight:height
                                 color:[UIColor clearColor]
                            showShadow:isShow];
}

+ (SeparatorCellData*)separatorDataWithHeight:(CGFloat)height
                                        color:(UIColor*)color
                                   showShadow:(BOOL)isShow {
  LC_CHECK_TRUE(height > 0);
  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftInset:0 backgroundColor:color];
  separatorCellData.cellHeight = height;
  separatorCellData->_type = SEPARATORY_TYPE_NO_INSET;
  separatorCellData->_showShadow = isShow;
  return separatorCellData;
}

+ (SeparatorCellData*)dottedLinelSeparatorDataWithLeftInset:(CGFloat)leftInset {
  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftInset:leftInset backgroundColor:LC_COLOR_WHITE];
  separatorCellData->_type = SEPARATORY_TYPE_DOTTED_LINE;
  return separatorCellData;
}

+ (SeparatorCellData*)dottedLinelSeparatorDataWithLeftInset:(CGFloat)leftInset
                                                 rightInset:(CGFloat)rightInset {
  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftInset:leftInset backgroundColor:LC_COLOR_WHITE];
  separatorCellData->_rightInset = rightInset;
  separatorCellData->_type = SEPARATORY_TYPE_DOTTED_LINE;
  return separatorCellData;
}

- (UIImage*)dottedLineImage {
  if (_dottedLineImage == nil) {
    UIImage* image = [UIImage imageNamed:@"dotted_line"];
    image = [image resizableImageWithCapInsets:UIEdgeInsetsZero
                                  resizingMode:UIImageResizingModeTile];
    _dottedLineImage = image;
  }
  return _dottedLineImage;
}

- (void)refreshCell:(SeparatorCell*)cell {
  [super refreshCell:cell];
  
  cell.backgroundColor = self.backgroundColor;
  
  cell.lineImageViewLeftInset.constant = self.leftInset;
  cell.lineImageViewRightInset.constant = self.rightInset;
  cell.lineImageView.backgroundColor = self.lineColor;
  cell.lineImageView.image = nil;
  
  cell.lineImageView.hidden = NO;
  cell.shadowImageView.hidden = YES;
  
  switch (self.type) {
    case SEPARATORY_TYPE_NORMAL:
      break;
    case SEPARATORY_TYPE_NORMAL_INSET:
      cell.lineImageViewHeightCst.constant = cell.height;
      break;
    case SEPARATORY_TYPE_NO_INSET:
      cell.lineImageView.hidden = YES;
      cell.shadowImageView.hidden = !self.showShadow;
      break;
    case SEPARATORY_TYPE_DOTTED_LINE:
      cell.lineImageView.backgroundColor = nil;
      cell.lineImageView.image = self.dottedLineImage;
      break;
    default:
      LC_FAIL();
      break;
  }
}

+ (NSArray*)insertSeparatorCellData:(SeparatorCellData*)separatorCellData
                    acrossCellDatas:(NSArray*)cellDatas
                            addLast:(BOOL)addLast {
  LC_CHECK_NOT_NULL(separatorCellData);
  if (cellDatas.count == 0 || (cellDatas.count == 1 && !addLast)) {
    return cellDatas;
  }
  NSMutableArray* array = [NSMutableArray
      arrayWithCapacity:cellDatas.count * 2 - (addLast ? 0 : 1)];
  for (int i = 0; i < cellDatas.count; ++i) {
    [array addObject:cellDatas[i]];
    if (i != cellDatas.count - 1 || addLast) {
      [array addObject:separatorCellData];
    }
  }
  LC_CHECK_TRUE(array.count == cellDatas.count * 2 - (addLast ? 0 : 1));
  return array;
}

+ (NSArray*)groupOfCellDatas:(NSArray*)cellDatas
      groupSeparatorCellData:(SeparatorCellData*)groupSeparatorCellData
     middleSeparatorCellData:(SeparatorCellData*)middleSeparatorCellData {
  LC_CHECK_NOT_NULL(groupSeparatorCellData);
  LC_CHECK_NOT_NULL(middleSeparatorCellData);
  return [self groupOfCellDatas:cellDatas
         groupSeparatorCellData:groupSeparatorCellData
         titleSeparatorCellData:nil
        middleSeparatorCellData:middleSeparatorCellData];
}

+ (NSArray*)groupOfCellDatas:(NSArray*)cellDatas
      groupSeparatorCellData:(SeparatorCellData*)groupSeparatorCellData
      titleSeparatorCellData:(SeparatorCellData*)titleSeparatorCellData
     middleSeparatorCellData:(SeparatorCellData*)middleSeparatorCellData {
  LC_CHECK_NOT_NULL(groupSeparatorCellData);
  LC_CHECK_NOT_NULL(middleSeparatorCellData);
  return [self groupOfCellDatas:cellDatas
         groupSeparatorCellData:groupSeparatorCellData
         titleSeparatorCellData:titleSeparatorCellData
        middleSeparatorCellData:middleSeparatorCellData
                forceAllCanHide:NO];
}

+ (NSArray*)groupOfCellDatas:(NSArray*)cellDatas
      groupSeparatorCellData:(SeparatorCellData*)groupSeparatorCellData
      titleSeparatorCellData:(SeparatorCellData*)titleSeparatorCellData
     middleSeparatorCellData:(SeparatorCellData*)middleSeparatorCellData
             forceAllCanHide:(BOOL)forceAllCanHide {
  LC_CHECK_NOT_NULL(groupSeparatorCellData);
  LC_CHECK_NOT_NULL(middleSeparatorCellData);
  if (cellDatas.count == 0) {
    return cellDatas;
  }
  NSMutableArray* array =
      [NSMutableArray arrayWithCapacity:cellDatas.count * 2 + 1];
  if (forceAllCanHide) {
    SeparatorCellData* group = groupSeparatorCellData.copy;
    group.canHide = YES;
    [array addObject:group];
  } else {
    [array addObject:groupSeparatorCellData];
  }
  for (int i = 0; i < cellDatas.count; ++i) {
    [array addObject:cellDatas[i]];
    SeparatorCellData* separatorCellData = middleSeparatorCellData;
    if (i == 0 && titleSeparatorCellData != nil) {
      separatorCellData = titleSeparatorCellData;
    }
    if (i != cellDatas.count - 1) {
      if (forceAllCanHide || [cellDatas[i + 1] canHide]) {
        separatorCellData = separatorCellData.copy;
        separatorCellData.canHide = YES;
        separatorCellData.hidden = [cellDatas[i + 1] hidden];
        [array addObject:separatorCellData];
      } else {
        [array addObject:separatorCellData];
      }
    }
  }
  if (forceAllCanHide) {
    SeparatorCellData* group = groupSeparatorCellData.copy;
    group.canHide = YES;
    [array addObject:group];
  } else {
    [array addObject:groupSeparatorCellData];
  }
  LC_CHECK_TRUE(array.count == cellDatas.count * 2 + 1);
  return array;
}

+ (NSArray*)groupOfCellDatas:(NSArray*)cellDatas
     beforeSeparatorCellData:(SeparatorCellData*)beforeSeparatorCellData
      afterSeparatorCellData:(SeparatorCellData*)afterSeparatorCellData
             forceAllCanHide:(BOOL)forceAllCanHide {
  NSMutableArray* array =
      [NSMutableArray arrayWithCapacity:cellDatas.count * 2];

  for (int i = 0; i < cellDatas.count; ++i) {
    BOOL hidden = [cellDatas[i] hidden];
    if (beforeSeparatorCellData != nil) {
      if (forceAllCanHide) {
        SeparatorCellData* before = beforeSeparatorCellData.copy;
        before.canHide = YES;
        before.hidden = hidden;
        [array addObject:before];
      } else {
        [array addObject:beforeSeparatorCellData];
      }
    }
    [array addObject:cellDatas[i]];
    if (afterSeparatorCellData != nil) {
      if (forceAllCanHide) {
        SeparatorCellData* after = afterSeparatorCellData.copy;
        after.canHide = YES;
        after.hidden = hidden;
        [array addObject:after];
      } else {
        [array addObject:afterSeparatorCellData];
      }
    }
  }
  return array;
}

- (id)copyWithZone:(NSZone*)zone {
  SeparatorCellData* cellData = [[self.class alloc] init];
  cellData->_type = self.type;
  cellData->_lineColor = self.lineColor;
  cellData->_backgroundColor = self.backgroundColor;
  cellData->_leftInset = self.leftInset;
  cellData->_rightInset = self.rightInset;
  cellData->_showShadow = self.showShadow;

  cellData.cellAdapter = self.cellAdapter;
  cellData.onCellClickedHandler = self.onCellClickedHandler;
  cellData.group = self.group;
  cellData.canHide = self.canHide;
  cellData.hidden = self.hidden;
  cellData.lastMemberCellData = self.lastMemberCellData;

  cellData.clickable = self.clickable;
  cellData.transparency = self.transparency;
  cellData.cellHeight = self.cellHeight;
  return cellData;
}

- (Class)cellClazz {
  return SeparatorCell.class;
}

- (NSString*)nibName {
  return @"SeparatorCell";
}

@end

